<?php

class StudentPlace_NumberQuickEditController extends Zend_Controller_Action
{

    public function init()
    {
        $this->view->orderItemArr =
        array( 'sex', 'student_id', 'number', 'name', 'birthday');
        $this->view->orderRule = array('ASC','DESC');
    }

    public function indexAction()
    {
        if ($this->_getParam('act')) {
            $year = $this->view->Semester['year'];
            $semester = $this->view->Semester['semester'];
            $tempArr = explode('-', $this->_getParam('classSn'));
            $grade = (int) $tempArr[0];
            $classSn = (int) $tempArr[1];

            foreach ($this->_getParam('newNumber') as $studentSn=>$number) {
                 Doctrine_Query::create()
                ->update('Student_Model_StudentSemester')
                ->set('number','?',(int) $number)
                ->where('student_sn=?', $studentSn)
                ->andWhere('year=?', $year)
                ->andWhere('semester=?', $semester)
                ->andWhere('grade=?', $grade)
                ->andWHere('class_sn=?', $classSn)
                ->execute();
            }
            $this->view->updated = true;
        }

        if ($this->_getParam('classSn')) {
            $classSn = $this->_getParam('classSn');
            $tempArr = explode('-', $classSn);
            $grade = (int) $tempArr[0];
            $class_sn = (int) $tempArr[1];

            $this->view->classSn = $classSn ;
            $q = Doctrine_Query::create()
            ->select('a.sn, a.name, a.birthday, a.sex, b.student_id AS student_id, b.number AS number')
            ->from('Student_Model_Student a')
            ->leftJoin('a.Student_Model_StudentSemester b')
            ->where('a.study_condition=?', 0)
            ->andWhere('b.year=?', $this->view->Semester['year'])
            ->andWhere('b.semester=?', $this->view->Semester['semester'])
            ->andWhere('b.grade=?', $grade)
            ->andWhere('b.class_sn=?', $class_sn);
            $orderStr = array();
            $orderParam = $this->_getParam('order');
            foreach($this->_getParam('priority') as $key=>$priority) {
                if ($priority !=='' and isset($orderParam[$key])) {
                    switch ($priority) {
                        case 'sex':
                            $orderStr[] = ' a.sex '.$orderParam[$key];
                            break;
                        case 'student_id':
                            $orderStr[] = ' b.student_id '.$orderParam[$key];
                            break;
                        case 'number':
                            $orderStr[] = ' b.number '.$orderParam[$key];
                            break;
                        case 'name':
                            $orderStr[] = ' a.name '.$orderParam[$key];
                            break;
                        case 'birthday':
                            $orderStr[] = ' a.birthday '.$orderParam[$key];
                            break;
                    }
                }
                if ($orderStr) {
                    $temp = implode(',', $orderStr);
                    $q->orderBy($temp);
                }
                else
                $q->orderBy('b.number');
            }
            $res = $q->fetchArray();
            $this->view->data = $res;
        }

        if ($this->_getParam('priority'))
        $this->view->priority = $this->_getParam('priority');
        else
        $this->view->priority = array();

        if ($this->_getParam('order'))
        $this->view->order = $this->_getParam('order');
        else
        $this->view->order = array();

    }


}

